//
//  BaseService.m
//  NetTest
//
//  Created by mac on 12-5-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BaseService.h"
#import "FCCommon.h"

@implementation BaseService

-(id)init{
    if ([super init]) {
        
    }
    return self;
}

#pragma mark AddModel

-(void)addModel:(BaseModel*)model{
    unsigned int numofvars = 0;
    NSString *sql;
    
    NSString *vals=@"";
    
    NSString *values=@"";
    
    Ivar* ivars=class_copyIvarList([model class],&numofvars);
    int i=0;
    for(const Ivar* p = ivars; p< ivars+numofvars;p++){
        Ivar const ivar=*p;
        const char* keytype=ivar_getTypeEncoding(ivar);
        
        NSString* keyTypeValue  = [[NSString stringWithUTF8String:keytype] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        
        
        FCLOG(@"%i:%@",(++i),keyTypeValue);
        
        NSString* key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        FCLOG(@"val = %@",key);
        if ([model valueForKey:key]!=nil) {
            vals=[NSString stringWithFormat:@"%@,%@",vals,key];
            values=[NSString stringWithFormat:@"%@,\'%@\'",values,[model valueForKey:key]];
        }
        
        FCLOG(@"vals =%@ ,values = %@ ",vals,values);
    }
    
    if (![values isEqualToString:@""]) {
        values = [values substringFromIndex:1];
    }
    if (![vals isEqualToString:@""]) {
        vals=[vals substringFromIndex:1];
    }
    sql=[NSString stringWithFormat:@"insert into %@ (%@) values (%@)",tablename_,vals,values];
    FCLOG(@"sql = %@ ",sql);
    [[Sqlite3Helper Instance] executeNonQuery:sql];
    FCLOG(@"Add Success");
}

#pragma mark UpdateModel
-(void)updateModel:(BaseModel*)model{
    
}

#pragma mark Delete
-(bool)deleteModel:(NSString *)pkid{
     NSString *sql=[NSString stringWithFormat:@"delete from %@ ",tablename_];
    if (pkid!=nil&&![pkid isEqualToString:@""]) {
        sql =[sql stringByAppendingFormat:@" where %@=\'%@\'",tablePK_,pkid];
    }
    [[Sqlite3Helper Instance] openDB];
    [[Sqlite3Helper Instance] executeNonQuery:sql];
    [[Sqlite3Helper Instance] closeDB];
    return true;
}
-(bool)deleteAll{
    NSString *sql=[NSString stringWithFormat:@"delete from %@ ",tablename_];
    [[Sqlite3Helper Instance] openDB];
    [[Sqlite3Helper Instance] executeNonQuery:sql];
    [[Sqlite3Helper Instance] closeDB];
    FCLOG(@"Delete Success");
    return true;
}
#pragma mark GetModel

-(BOOL)isExist:(NSString*)pkid{
    NSString *strwhere=[NSString stringWithFormat:@"%@ = \'%@\'",tablePK_,pkid];
    int count=[self getCountBystrwhere:strwhere];
    return count!=0;
}

-(int)getCountBystrwhere:(NSString*)strwhere{
    NSString *strsql=[NSString stringWithFormat:@"select count(%@) as c from %@ where 1=1 ",tablePK_,tablename_];
    
    if (strwhere!=nil&&![strwhere isEqualToString:@""]) {
        strsql = [strsql stringByAppendingFormat:@" and %@",strwhere];
    }
    
    sqlite3_stmt *stmt = [[Sqlite3Helper Instance] execQuery:strsql];
    
    if (stmt!=NULL) {
        if (sqlite3_step(stmt)) {
            int count=sqlite3_column_int(stmt, 0);
            sqlite3_finalize(stmt);
            return count;
        }
    }
    return 0;
}

-(id)getModel:(NSString *)pkid{
    return nil;
}

- (id)convertRowToModel:(Class)cls stmt:(sqlite3_stmt *)stmt {
    id model=nil;
    model = [[[cls alloc] init] autorelease];
    
    int coloumncount=sqlite3_column_count(stmt);
    
    for (int i=0; i<coloumncount; i++) {
        char* colname=(char*)sqlite3_column_name(stmt, i);
        
        NSString* strcolname=[NSString stringWithUTF8String:colname];
        
        char* deltyp = (char*)sqlite3_column_decltype(stmt, i);
        
        FCLOG(@"chartype = %s",deltyp);
        
        int ctype=sqlite3_column_type(stmt, i);
        
        FCLOG(@"Inttype = %i",ctype);
        
        
        char* cvalue=(char*)sqlite3_column_text(stmt, i);
        
        if (cvalue!=NULL) {
            [model setValue:[NSString stringWithUTF8String:cvalue] forKey:strcolname];
        }
    }
    return model;
}

-(id)getModel:(NSString *)pkid returnClass:(Class)cls{
    
    NSString *sql=[NSString stringWithFormat:@"select * from %@ where %@ = '%@'",tablename_,tablePK_,pkid];
    
    FCLOG(@"GetModel Sql =%@",sql);
    [[Sqlite3Helper Instance] openDB];
    sqlite3_stmt* stmt = [[Sqlite3Helper Instance] execQuery:sql];
    [[Sqlite3Helper Instance] closeDB];
    
    id model=nil;
    if (stmt!=nil) {
        if (sqlite3_step(stmt)==SQLITE_ROW) {
            model = [self convertRowToModel:cls stmt:stmt];
        }

        sqlite3_finalize(stmt);
    }
    
    return model;
}

-(NSMutableArray*)getCollection:(NSString*)strWhere{
    return nil;
}

-(NSMutableArray*)getCollection:(NSString *)strWhere orderby:(NSString *)orderby startIndex:(int)startIndex count:(int)count{
    return nil;
}

-(NSMutableArray*)getCollection:(NSString *)strWhere orderby:(NSString *)orderby startIndex:(int)startIndex count:(int)count cls:(Class)cls{
    
    NSString *sql=[NSString stringWithFormat:@"select * from %@ ",tablename_];
    
    if (strWhere!=nil&&![strWhere isEqualToString:@""]) {
        sql=[sql stringByAppendingFormat:@" where %@",strWhere];
    }
    
    if (orderby!=nil&&![orderby isEqualToString:@""]) {
        sql =[sql stringByAppendingFormat:@" order by %@ ",orderby];
    }
    
    if (count>0) {
        sql = [sql stringByAppendingFormat:@" limit %i , %i ",startIndex,count];
    }
    
    FCLOG(@"GetModel Sql =%@",sql);

    
    sqlite3_stmt* stmt = [[Sqlite3Helper Instance] execQuery:sql];
    
    
    id model=nil;
    NSMutableArray* arr=[NSMutableArray array];
    if (stmt!=nil) {
        while (sqlite3_step(stmt)==SQLITE_ROW) {
            model = [self convertRowToModel:cls stmt:stmt];
            [arr addObject:model];
        }
        
        sqlite3_finalize(stmt);
    }
    return arr;
    
}

- (NSMutableArray*)getCollection:(NSString *)strWhere orderby:(NSString *)orderby cls:(Class)cls {
    return [self getCollection:strWhere orderby:orderby startIndex:0 count:0 cls:cls];
}

-(NSMutableArray*)getCollection:(NSString*)strWhere returnClass:(Class)cls{
    
    NSString* orderby=nil;
    
    return [self getCollection:strWhere orderby:orderby cls:cls];
}

-(NSMutableArray*)getAll{
    return nil;
}

-(void)dealloc{
    [super dealloc];
}
@end
